<?php
/**
 * soap操作ERP
 * @author Cuil
 * @date 2016-03-01
 */
class CI_soaps{
	//wsdl
    public $wsdl = "";
    public $soap = "";
	private $ci;
    private $moblie = "";
    private $storeid = "";
    /**
     * 构造函数
     */
    public function __construct($arr = array()) {

        $wsdl = '';
		
        if(!empty($arr)){
           extract($arr);
        }
        $this->wsdl = empty($wsdl) ? $this->wsdl : $wsdl;
        $this->soap = new SoapClient($this->wsdl);
        $CI =& get_instance();
        $this->ci = $CI;
    }

    /**
     * 获取学员信息
     * @param $key (string) ERP-KEY
	 * @param $storeId (string) 分中心key
	 * @param $mobile (string) 手机号
	 * @return array
     */
    public function GetMemberInfo($key='',$storeId='',$mobile=''){
    	$arr = array('key'=>$key,'storeId'=>$storeId,'mobile'=>$mobile);
    	$result = $this->soap->GetMemberInfo($arr);
		return $this->object_array($result);
    }
    /*添加学员信息
     *$array(
     * Key string  keyy
     * StoreId  分中心id
     * RealName  学员真实姓名
     * NickName  学员昵称
     *  Guardian   家长姓名
     *  Sex       性别（值：男/女
     *  BirthDate    生日（例：2013-01-01）
     *  Address     住址
     *  Phone      固定电话
     *  Mobile    学员手机号码
     *  Mobile1    学员备用手机号码
     *  Msn     Msn
     *  QQ       QQ
     *  Email
     *  BasicInfo             基本情况
     *  Notes       备注
     *
     */
    public function AddMember($data){
        if(empty($data['mobile'])) return False;
        $arr = array(
            'key'      =>  empty($data['key']) ? '' : $data['key'],
            'storeId'  =>  empty($data['storeId']) ? '' : $data['storeId'],
            'realName' =>  empty($data['realName']) ? '' : $data['realName'],
            'nickname' =>  empty($data['nickname']) ? '' : $data['nickname'],
            'guardian' =>  empty($data['guardian']) ? '' : $data['guardian'],
            'sex'      =>  empty($data['sex']) ? '' : $data['sex'],
            'birthDate'=>  empty($data['birthDate']) ? '' : $data['birthDate'],
            'address'  =>  empty($data['address']) ? '' : $data['address'],
            'phone'    =>  empty($data['phone']) ? '' : $data['phone'],
            'mobile'   =>  empty($data['mobile']) ? '' : $data['mobile'],           
            'mobile1'  =>  '',
            'msn'      =>  empty($data['msn']) ? '' : $data['msn'],
            'qq'       =>  empty($data['qq']) ? '' : $data['qq'],
            'email'    =>  empty($data['email']) ? '' : $data['email'],
            'basicInfo'=>  empty($data['basicInfo']) ? '' : $data['basicInfo'],
            'notes'    =>  empty($data['notes']) ? '' : $data['notes']
        );
        $result = $this->soap->AddMember($arr);
		return get_object_vars($result);
    }
    /**
     * 获取分中心列表
     * @param $key (string) ERP-KEY
	 * @return array
     */
    public function GeStoreList($key=''){
    	$arr = array('key'=>$key);
    	$result = $this->soap->GeStoreList($arr);
		return $this->object_array($result);
 
    }

    /**
     * 获取课程列表
     * @param $key (string) ERP-KEY
     * @param $storeId (string) 分中心ID
     * @param $startDateStr (string) 开始时间 2016-03-02
     * @param $endDateStr (string) 结束时间 2016-03-02
     */
    public function CourseSchedule($key='',$storeId='',$stareDteStr='',$endDateStr='',$source='',$uid=''){
        $arr = array(
                'key'=>$key,
                'storeId'=>$storeId,
                'startDateStr'=>$stareDteStr,
                'endDateStr'=>$endDateStr
            );
        $result = $this->soap->CourseSchedule($arr);
        $list = $this->object_array($result);
        if(empty($list['CourseScheduleResult']) || empty($list['CourseScheduleResult']['Course'])){
            return array();
        }
        $flist = $list['CourseScheduleResult']['Course'];
        if(isset($flist[0])){
            $Nlist = $this->my_sort($flist,'ClassSectionTime',SORT_ASC,SORT_STRING);
        }else{
            $Nlist = $flist;
        }
    
        switch ($source) {
            case '1':
                return $this->ios_arr($Nlist,$uid);
                break;
            case '2':
                return $this->android_arr($Nlist,$uid);
                break;
            default:
                return array();
                break;
        }
    }

    /**
     * IOS课程列表处理
     * @param  array  $list [原始数据]
     * @return [array]       [处理数据]
     */
    function ios_arr($list =array(),$uid=''){
        $s = $this->android_arr($list,$uid);
        $n = array();
        foreach($s as $k => $v){
            $r = array();
            $r['time'] = $v[0]['ClassSectionTime'];
            $r['value'] = $v;
            $n[] = $r;
        }
        
        return $n;
    }

    /**
     * android课程数据处理
     * @param  array  $list [原始数据]
     * @return [array]       [处理数据]
     */
    function android_arr($list = array(),$uid=''){
        $newlist = array();
        if(isset($list[0])){
            foreach($list as $key => $val){
                $row = explode('-',$val['ClassSectionTime']);
                $val['StartTime'] = $row[0];
                $val['EndTime'] = $row[1];
                $val['DateWeek'] = $this->getweek($val['CourseDate']);
                $val['IsBespeak'] = $this->IsBespeak($val,$uid);
                $newlist[$val['ClassSectionTime']][] = $val;
            }
        }else{
            $rows = $list;  
            $row = explode('-',$rows['ClassSectionTime']);
            $rows['StartTime'] = $row[0];
            $rows['EndTime'] = $row[1];
            $rows['DateWeek'] = $this->getweek($rows['CourseDate']);
            $rows['IsBespeak'] = $this->IsBespeak($rows,$uid);
            $newlist[$rows['ClassSectionTime']][] = $rows;
        }
        return $newlist;
    }

    /**
     * 计算是否可以预约
     * @param [array] $rows [单条课程数组]
     */
    function IsBespeak($rows,$uid=''){
        //注释掉已预约判断 by Cuil 2016-03-24
        /*$this->ci->load->model('M_bespeak');
        $isb = $this->ci->M_bespeak->getRow('id',array('uid'=>$uid,'courseGuid'=>$rows['CourseGuid']));
        if($isb){
            return "2";
        }*/
        if($rows['MemberCount']<=$rows['RealMemberCount']){
            return "0";
        }
        $time = substr($rows['CourseDate'],0,10).' '.$rows['StartTime'];
        if(strtotime($time)<=time()){
            return "0";
        }
        return "1";
    }

    /**
     * 预约课程
     * @param $key (string) ERP-KEY
     * @param $storeId (string) 分中心ID
     * @param $mobile (string) 手机号
     * @param $courseGuid (string) 课程ID
     * @param $makeUp (int) 是否固定上课
     */
    public function ScheduleLesson($key='',$storeId='',$mobile='',$courseGuid='',$makeUp=0){
        $arr = array(
                'key'=>$key,
                'storeId'=>$storeId,
                'mobile'=>$mobile,
                'courseGuid'=>$courseGuid,
                'makeUp'=>$makeUp
            );
        $result = $this->soap->ScheduleLesson($arr);
        return $this->object_array($result);
    }

    /**
     * 上课记录
     * @param $key (string) ERP-KEY
     * @param $storeId (string) 分中心ID
     * @param $mobile (string) 手机号
     * @param $startDateStr (string) 开始时间
     * @param $endDateStr (string) 结束时间
     * @param $isSigned (string) 是否签到 0未签 1已签
     */
    public function GetMemberCourse($key='',$storeId='',$mobile='',$startDateStr='',$endDateStr='',$isSigned=1){
        $this->mobile = $mobile;
        $this->storeid = $storeId;
        $arr = array(
                'key'=>$key,
                'storeId'=>$storeId,
                'mobile'=>$mobile,
                'startDateStr'=>$startDateStr,
                'endDateStr'=>$endDateStr,
            );
        $list = $this->getcourse($arr,1);
        $list1 = $this->getcourse($arr,0);

        $newlist = array_merge($list,$list1);
        if(!$newlist){
            return array();
        }
        return $this->my_sort($newlist,'courseDate',SORT_DESC,SORT_STRING);
    } 

    /**
     * 获取我的上课记录
     * @param array
     */
    function getcourse($arr,$isSigned=1){
        $arr['isSigned'] = $isSigned;
        $result = $this->soap->GetMemberCourse($arr);
        return $this->toarray($result);

    }

    /**
     * 上课记录转换数组
     */
    function toarray($obj){
        if(empty($obj->GetMemberCourseResult->MemberCourseInfo)){
            return array();
        }
        $new = array();
        $isc = $obj->GetMemberCourseResult->MemberCourseInfo;
        if(isset($isc->CourseGuid)){
            $rows = array(
                        'CourseGuid' => $isc->CourseGuid,
                        'RealName' => $isc->RealName,
                        'CourseName' => $isc->CourseName,
                        'courseDate' => $isc->courseDate,
                        'ClassroomName' => $isc->ClassroomName,
                        'LessonSeriesName'=>$isc->LessonSeriesName,
                        'Teacher' => $isc->Teacher,
                        'Assistant' => $isc->Assistant,
                        'IsMakeUp' => $isc->IsMakeUp,
                        'Status' => $isc->Status,
                        'CourseTime'=>rand(10,99).$this->datetotime($isc->courseDate),
                        'IsLeave'=>(string)($this->IsLeave($isc->courseDate,$isc->Status,$isc->LessonSeriesName))
                    );
                $new[] = $rows;
        }else{
            foreach($obj->GetMemberCourseResult->MemberCourseInfo as $val){
                $rows = array(
                        'CourseGuid' => $val->CourseGuid,
                        'RealName' => $val->RealName,
                        'CourseName' => $val->CourseName,
                        'courseDate' => $val->courseDate,
                        'ClassroomName' => $val->ClassroomName,
                        'LessonSeriesName'=>$val->LessonSeriesName,
                        'Teacher' => $val->Teacher,
                        'Assistant' => $val->Assistant,
                        'IsMakeUp' => $val->IsMakeUp,
                        'Status' => $val->Status,
                        'CourseTime'=>rand(10,99).$this->datetotime($val->courseDate),
                        'IsLeave'=>(string)($this->IsLeave($val->courseDate,$val->Status,$val->LessonSeriesName))
                    );
                $new[] = $rows;
            }
        }
        
        return $new;
    }

    /**
     * [请假]
     * @param string $key        [ERP-KEY]
     * @param string $storeId    [分中心ID]
     * @param string $mobile     [手机号]
     * @param string $CourseGuid [课程ID]
     * @param string $reason     [请假原因]
     */
    public function CourseLeave($key='',$storeId='',$mobile='',$courseGuid='',$reason=''){
        $arr = array(
                'key'=>$key,
                'storeId'=>$storeId,
                'mobile'=>$mobile,
                'courseGuid'=>$courseGuid,
                'reason'=>$reason
            );
        $result = $this->soap->CourseLeave($arr);
        return $this->object_array($result);
    }

    /**
     * 截取转换时间戳
     * @param  [string]  $date   [时间字符串2016-03-03 19:00-21:00]
     * @param  integer $istype [0返回时间戳 1返回1,0]
     * @return [type]          [description]
     */
    function datetotime($date,$istype=0){
        $time = strtotime(substr($date,0,16));
        if($istype){
            $time = ($time>time()) ? 1 : 0;
        }
        return $time;
    }

    /**
     * 上课记录是否可以请假
     * @param  [string]  $date   [时间字符串2016-03-03 19:00-21:00]
     * @param  [string] $status [状态]
     * @return [type]     $isl     [0不可请假 1可请假]
     */
    function IsLeave($date,$status,$LessonName=''){
        $isl = "0";
        $time = strtotime(substr($date,0,16));
        $dtime = time()+3600*12;
        //$isl = ($time>$dtime) ? 1 : 0;
        if($time>$dtime){
            switch ($status) {
                case '请假':
                    $isl = "0";
                    break;
                case '准时':
                    $isl = "0";
                    break;
                default:
                    $isl = "1";
                    break;
            }
            if($isl==1){
                $this->ci->load->library('Curls');
                $lnarr = $this->ci->curls->GetCurrentClassHours($this->storeid, $this->mobile);
                $flag = 0;
                foreach($lnarr as $key =>$val){
                    if($key==$LessonName && $val>0){
                        $flag = 1;
                        break;
                    }
                }
                $isl = $flag ? "1" : "0";
                if($LessonName==''){
                    $isl ="0";
                }
            }
        }else{
            $isl = "0";
        }
        return $isl;
    }

    /**
     * 对象转换数组
     * @param  $arr (object) 对象
     * @return $newarr      数组
     */
    public function object_array($arr){
    	function objectToArray($obj){
		    $arr = is_object($obj) ? get_object_vars($obj) : $obj;
		    if(is_array($arr)){
		        return array_map(__FUNCTION__, $arr);
		    }else{
		        return $arr;
		    }
		}
		return objectToArray($arr);
    }

    /**
     * 获取周几
     * @param $date 日期 2015-03-04
     */
    function getweek($date){
        $week = date('w',strtotime($date));
        $das = date('Y-m-d',strtotime($date));
        switch($week){
            case 1:
                return $das."(周一)";
                break;
            case 2:
                return $das."(周二)";
                break;
            case 3:
                return $das."(周三)";
                break;
            case 4:
                return $das."(周四)";
                break;
            case 5:
                return $das."(周五)";
                break;
            case 6:
                return $das."(周六)";
                break;
            case 0:
                return $das."(周日)";
                break;
        }
    }

    /**
     * 排序
     * @param  [type] $arrays     [description]
     * @param  [type] $sort_key   [description]
     * @param  [type] $sort_order [description]
     * @param  [type] $sort_type  [description]
     * @return [type]             [description]
     */
    function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){   
        if(is_array($arrays)){   
            foreach ($arrays as $array){   
                if(is_array($array)){   
                    $key_arrays[] = $array[$sort_key];   
                }else{   
                    return false;   
                }   
            }   
        }else{   
            return false;   
        }  
        array_multisort($key_arrays,$sort_order,$sort_type,$arrays);   
        return $arrays;   
    } 
	
	/**
     * 获取剩余课时列表
     * @param $key (string) ERP-KEY
	 * @return array
     */
    public function GetCurrentClassHours($key='',$storeId='',$mobile=''){
    	$arr = array('key'=>$key,'storeId'=>$storeId,'mobile'=>$mobile);
    	$result = $this->soap->GetCurrentClassHours($arr);
		return $this->object_array($result);
    }
}
?>
